package com.jiyinit.web.core.activiti.service;

import com.jiyinit.web.core.activiti.dao.TabLeaveBillInfoDao;
import com.jiyinit.web.core.activiti.dao.TabLeaveDealMindDao;
import com.jiyinit.web.core.activiti.enums.ProcessFlowEnum;
import com.jiyinit.web.core.activiti.vo.NeedDealTaskEntity;
import com.jiyinit.web.core.activiti.dto.NeedDealTaskRequest;
import com.jiyinit.web.core.activiti.entity.TabLeaveBillInfoEntity;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author jack zh
 * @version 1.0
 * @date 2018/7/19
 * @since 1.0
 */
@Service("leaveDealServiceImpl")
public class LeaveDealServiceImpl {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(LeaveDealServiceImpl.class);

    @Resource(name = "core.tabLeaveDealMindDao")
    private TabLeaveDealMindDao tabLeaveDealMindDao;


    @Resource(name = "core.tabLeaveBillInfoDao")
    private TabLeaveBillInfoDao tabLeaveBillInfoDao;

    //工作流相关
    @Resource(name = "runtimeService")
    RuntimeService runtimeservice;
    @Resource(name = "identityService")
    IdentityService identityservice;
    @Resource(name = "taskService")
    TaskService taskservice;

    /**
     * 查询待处理任务
     * @param dealTaskRequest
     * @return
     */
    public List<NeedDealTaskEntity>  getNeedDealTask(NeedDealTaskRequest dealTaskRequest)
    {
        List<NeedDealTaskEntity> results=new ArrayList<NeedDealTaskEntity>();
        NeedDealTaskEntity needEntity=null;
        List<Task> tasks=taskservice.createTaskQuery().processDefinitionKey(ProcessFlowEnum.Leave.getCode()).taskCandidateOrAssigned(dealTaskRequest.getUserId()).listPage((dealTaskRequest.getPage()-1)*dealTaskRequest.getLimit(),dealTaskRequest.getLimit());
        for(Task task:tasks){
            String instanceid=task.getProcessInstanceId();
            ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
            String businesskey=ins.getBusinessKey();
            TabLeaveBillInfoEntity a=tabLeaveBillInfoDao.selectByPrimaryKey(Integer.parseInt(businesskey));
            needEntity=new NeedDealTaskEntity();
            BeanUtils.copyProperties(a,needEntity);

            /* 变更区 */
            needEntity.setTaskId(task.getId());
            needEntity.setName(task.getName());
            needEntity.setAssignee(task.getAssignee());
            needEntity.setProcessDefinitionId(task.getProcessDefinitionId());
            needEntity.setTaskDefinitionKey(task.getTaskDefinitionKey());
            needEntity.setCreateTime(task.getCreateTime());
            needEntity.setDueDate(task.getDueDate());
            needEntity.setSuspended(task.isSuspended());
            /* 变更结束 */

            results.add(needEntity);
        }
        return results;
    }

    /**
     * 查询已经处理过的任务(包含已同意和已拒绝的)
     * @param userId
     * @return
     */
    public List<NeedDealTaskEntity> getDealTasksByUserId(Integer userId){
        return tabLeaveDealMindDao.getDealTasksByUserId(userId);
    }
}
